package com.service.Impl;

import com.entity.Category;
import com.exception.AddException;
import com.exception.DateNotFoundException;
import com.mapper.CategoryMapper;
import com.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CategoryServiceImpl
 * @Description TODO
 * @Author Kx
 * @Date 2025/5/21 16:29
 * @Version 1.0
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    /**
     * @author: kx
     * @date: 2025/5/21 16:30
     * @return java.util.List<com.entity.Category>
     * @description: 查询所有商品分类信息
    */
    @Override
    public List<Category> findAll() {
        if (!categoryMapper.findAll().isEmpty()) {
            return categoryMapper.findAll();
        }
        throw new DateNotFoundException(1001,"没有查询到商品分类信息");
    }

    /**
     * @author: kx
     * @date: 2025/5/21 19:19
     * @param categoryId : 商品分类id
     * @return com.entity.Category
     * @description: 根据编号查询商品分类信息
    */
    @Override
    public Category findById(Integer categoryId) {
        if (categoryMapper.findById(categoryId) != null) {
            return categoryMapper.findById(categoryId);
        }
        throw new DateNotFoundException(1001, "商品分类信息不存在");
    }

    /**
     * @author: kx
     * @date: 2025/5/21 20:10
     * @param category : 商品分类信息
     * @description: 添加商品分类信息
    */
    @Transactional
    @Override
    public void addCategory(Category category) {
        try {
            categoryMapper.addCategory(category);
        } catch (Exception e) {
            throw new AddException(1002, "添加商品分类失败");
        }
    }
}
